package com.hoopchina.service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.List;
import android.content.Context;
import android.util.Log;

import com.hoopchina.meta.NewsItem;
import com.hoopchina.util.download.Downloader;
import com.hoopchina.util.xml.NewsList2String;

/**
 * 
 * @author Neusoft
 *
 */
public class BaiduNews  {
	/**
	 * 保存新闻的方法
	 * @param newsItems  对象集合
	 * @param filePath   文件保存路径
	 * @param saveFileName 文件名
	 * @throws Exception
	 */
	public static void localSave(List<NewsItem> newsItems,String filePath,String saveFileName) throws Exception{
		File savePath=new File(filePath);
		if (!Downloader.checkStorable(savePath)) {
			throw new Exception("the save path not available!");
		}
		if (isFolderExists(filePath)) {
			File file=new File(filePath,saveFileName);
			String aPath=file.getAbsolutePath();
			Log.d("--------filepath-----", aPath );
			FileOutputStream fos=null;
			fos=new FileOutputStream(file);
			OutputStreamWriter osw=new OutputStreamWriter(fos);
			osw.write(NewsList2String.produceXml(newsItems));
			osw.close();
		}
	}
	

	
	/**
	 * 判断目录是否存在的方法
	 */
	private static boolean isFolderExists(String strFolder) {
			File file=new File(strFolder);
			if (!file.exists()) {
				if (file.mkdirs()) {
					return true;
				}else {
					return false;
				}
			}
		 return true;
	}


	
}
